package com.quectel.cms.controller.entranceguard;


import cn.hutool.core.util.DesensitizedUtil;
import com.quectel.business.common.visitor.ApplyProcessingComponent;
import com.quectel.cms.controller.BaseController;
import com.quectel.cms.util.SessionHolder;
import com.quectel.constant.global.SystemConstants;
import com.quectel.constant.global.annotation.RequirePermissions;
import com.quectel.core.module.entranceguard.dto.EntranceGuardVisitorApplyRecordDto;
import com.quectel.core.module.entranceguard.dto.VisitorDto;
import com.quectel.core.module.entranceguard.service.EntranceGuardVisitorApplyRecordService;
import com.quectel.util.common.Page;
import com.quectel.util.common.Request;
import com.quectel.util.common.Response;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author rananxin
 * @date 2021-12-24 10:46:50
 */
@RestController
@RequestMapping("entranceGuardVisitorApplyRecord")
@Tag(name = "EntranceGuardVisitorApplyRecordController", description = "访客申请记录表相关api")
public class EntranceGuardVisitorApplyRecordController extends BaseController {

    @DubboReference
    private EntranceGuardVisitorApplyRecordService entranceGuardVisitorApplyRecordService;
    @Autowired
    private ApplyProcessingComponent applyProcessingComponent;

    private static final String ACCESSIBLE_SCOPES = "accessibleScopes";

    /**
     * 保存
     */
    @PostMapping("apply")
    @RequirePermissions(values = "entranceGuardVisitorApplyRecord:edit")
    @Operation(summary = "访客申请")
    public Response<Object> apply(@RequestBody EntranceGuardVisitorApplyRecordDto entranceGuardVisitorApplyRecordDto) {

        VisitorDto visitor = entranceGuardVisitorApplyRecordDto.getVisitor();
        fillSaveCommonData(entranceGuardVisitorApplyRecordDto);
        fillSaveCommonData(visitor);

        return applyProcessingComponent.applyHandler(visitor, entranceGuardVisitorApplyRecordDto
                , EntranceGuardVisitorApplyRecordDto.ApplyUserTypeEnum.ADMIN.getType(), SessionHolder.getSysUserId());
    }

    /**
     * 列表
     */
    @GetMapping("list")
    @RequirePermissions(values = "entranceGuardVisitorApplyRecord:list")
    @Operation(summary = "访客申请记录的分页查询列表")
    public Response<Page<EntranceGuardVisitorApplyRecordDto>> list(
            @Parameter(description = "从1开始的页码数") @RequestParam Integer pageNo,
            @Parameter(description = "每页多少条") @RequestParam Integer pageSize
    ) {
        //查询列表数据
        Request request = Request.configParams(Request.parseParams(httpServletRequest))
                .like("applyUsername").like("visitorName").like("visitorMobile");

        super.fillAuthorityData(request.getParams());

        if (request.isNotBlank(ACCESSIBLE_SCOPES)) {
            request.put(ACCESSIBLE_SCOPES, request.getString(ACCESSIBLE_SCOPES).split(","));
        }

        int total = entranceGuardVisitorApplyRecordService.queryTotal(request.getParams());

        List<EntranceGuardVisitorApplyRecordDto> list = null;
        if (total > 0) {
            list = entranceGuardVisitorApplyRecordService.queryList(request.getParams());
            for (EntranceGuardVisitorApplyRecordDto entranceGuardVisitorApplyRecordDto : list) {
                VisitorDto visitor = entranceGuardVisitorApplyRecordDto.getVisitor();
                if (visitor != null) {
                    visitor.setName(DesensitizedUtil.chineseName(visitor.getName()));
                    visitor.setMobile(DesensitizedUtil.mobilePhone(visitor.getMobile()));
                    visitor.setCardNo(DesensitizedUtil.idCardNum(visitor.getCardNo(), 2, 2));
                }
                entranceGuardVisitorApplyRecordDto.setApplyUsername(DesensitizedUtil.chineseName(entranceGuardVisitorApplyRecordDto.getApplyUsername()));
            }
        } else {
            list = new ArrayList<>();
        }

        Page<EntranceGuardVisitorApplyRecordDto> page = new Page<>(request.getPageNo(), request.getPageSize(), total, list);
        return Response.<Page<EntranceGuardVisitorApplyRecordDto>>ok().wrap(page);
    }


    /**
     * 信息
     */
    @GetMapping("info/{id}")
    @RequirePermissions(values = "entranceGuardVisitorApplyRecord:info")
    @Operation(summary = "查询单条访客申请记录记录")
    public Response<EntranceGuardVisitorApplyRecordDto> info(@PathVariable("id") Long id) {

        Map<String, Object> params = new HashMap<>();
        super.fillAuthorityData(params);
        params.put(SystemConstants.ENTITY_ID_FLAG, String.valueOf(id));
        if (entranceGuardVisitorApplyRecordService.queryTotal(params) <= 0) {
            return Response.error(SystemConstants.ILLEGAL_MSG);
        }

        EntranceGuardVisitorApplyRecordDto entranceGuardVisitorApplyRecordDto = entranceGuardVisitorApplyRecordService.selectById(id);

        return Response.<EntranceGuardVisitorApplyRecordDto>ok().wrap(entranceGuardVisitorApplyRecordDto);
    }

    /**
     * 保存
     */
    @PostMapping("save")
    @RequirePermissions(values = "entranceGuardVisitorApplyRecord:edit")
    @Operation(summary = "保存单条访客申请记录记录")
    public Response<Object> save(@RequestBody @Validated EntranceGuardVisitorApplyRecordDto entranceGuardVisitorApplyRecordDto) {

        //填充基础数据
        fillSaveCommonData(entranceGuardVisitorApplyRecordDto);

        entranceGuardVisitorApplyRecordService.save(entranceGuardVisitorApplyRecordDto);

        return Response.ok();
    }

    /**
     * 修改
     */
    @PostMapping("update")
    @RequirePermissions(values = "entranceGuardVisitorApplyRecord:edit")
    @Operation(summary = "修改单条访客申请记录记录")
    public Response<Object> update(@RequestBody @Validated EntranceGuardVisitorApplyRecordDto entranceGuardVisitorApplyRecordDto) {

        Map<String, Object> params = new HashMap<>();
        super.fillAuthorityData(params);
        params.put(SystemConstants.ENTITY_ID_FLAG, String.valueOf(entranceGuardVisitorApplyRecordDto.getId()));
        if (entranceGuardVisitorApplyRecordService.queryTotal(params) <= 0) {
            return Response.error(SystemConstants.ILLEGAL_MSG);
        }
        //填充基础数据
        fillEditCommonData(entranceGuardVisitorApplyRecordDto);

        entranceGuardVisitorApplyRecordService.updateById(entranceGuardVisitorApplyRecordDto);

        return Response.ok();
    }

    /**
     * 删除
     */
    @PostMapping("delete")
    @RequirePermissions(values = "entranceGuardVisitorApplyRecord:delete")
    @Operation(summary = "批量删除存在的访客申请记录")
    public Response<Object> delete(@RequestBody Long[] ids) {

        Map<String, Object> params = new HashMap<>();
        super.fillAuthorityData(params);

        for (Long id : ids) {

            params.remove(SystemConstants.ENTITY_ID_FLAG);
            params.put(SystemConstants.ENTITY_ID_FLAG, String.valueOf(id));

            if (entranceGuardVisitorApplyRecordService.queryTotal(params) <= 0) {
                return Response.error(SystemConstants.ILLEGAL_MSG);
            }
        }

        entranceGuardVisitorApplyRecordService.deleteBatch(ids);

        return Response.ok();
    }
}
